package com.xiaoshuidi.cloud.module.member.mapper.securitycheck;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshuidi.cloud.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xiaoshuidi.cloud.framework.mybatis.core.mapper.BaseMapperX;
import com.xiaoshuidi.cloud.module.member.controller.admin.securitycheck.vo.HazardLedgerPageRespVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.securitycheck.vo.SecurityCheckRectificationItemExportReqVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.securitycheck.vo.SecurityCheckRectificationItemRespVO;
import com.xiaoshuidi.cloud.module.member.pojo.securitycheck.SecurityCheckRectificationItemDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 安全检查整改项 Mapper
 *
 * @author hans.zhang
 */
@Mapper
public interface SecurityCheckRectificationItemMapper extends BaseMapperX<SecurityCheckRectificationItemDO> {

    default List<SecurityCheckRectificationItemDO> selectList(SecurityCheckRectificationItemExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<SecurityCheckRectificationItemDO>()
                .eqIfPresent(SecurityCheckRectificationItemDO::getSecurityCheckId, reqVO.getSecurityCheckId())
                .eqIfPresent(SecurityCheckRectificationItemDO::getTarget, reqVO.getTarget())
                .eqIfPresent(SecurityCheckRectificationItemDO::getHazardContext, reqVO.getHazardContext())
                .eqIfPresent(SecurityCheckRectificationItemDO::getHazardType, reqVO.getHazardType())
                .eqIfPresent(SecurityCheckRectificationItemDO::getRemediation, reqVO.getRemediation())
                .eqIfPresent(SecurityCheckRectificationItemDO::getCapital, reqVO.getCapital())
                .eqIfPresent(SecurityCheckRectificationItemDO::getWilfulness, reqVO.getWilfulness())
                .eqIfPresent(SecurityCheckRectificationItemDO::getTimeframe, reqVO.getTimeframe())
                .eqIfPresent(SecurityCheckRectificationItemDO::getHazardPhoto, reqVO.getPhoto())
                .eqIfPresent(SecurityCheckRectificationItemDO::getStatus, reqVO.getStatus())
                .eqIfPresent(SecurityCheckRectificationItemDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(SecurityCheckRectificationItemDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SecurityCheckRectificationItemDO::getId));
    }

    @Select("SELECT DISTINCT " +
            "mscp.name as programName, " +
            "mscp.check_work_unit as checkWorkUnit, " +
            "mscri.id as id, " +
            "mscri.hazard_context as hazardContext, " +
            "mscri.hazard_address as hazardAddress, " +
            "mscri.hazard_type as hazardType, " +
            "mscri.remediation as remediation, " +
            "mscri.target as target, " +
            "mscri.status as status, " +
            "mscri.capital as capital, " +
            "mscri.wilfulness as wilfulness, " +
            "mscri.responsible_unit AS responsibleUnit, " +
            "mscri.timeframe as timeframe, " +
            "mscri.remark as remark, " +
            "mscri.create_time as createTime, " +
            "mscri.security_check_id as securityCheckId " +
            "FROM " +
            "`member_security_check_rectification_item` mscri " +
            "LEFT JOIN member_security_check msc ON mscri.security_check_id = msc.id  " +
            "LEFT JOIN member_security_check_program mscp ON mscp.id = msc.security_check_program_id  " +
            "WHERE " +
            "mscri.deleted = FALSE  " +
            "AND msc.deleted = FALSE  " +
            "AND mscp.deleted = FALSE " +
            "AND mscri.security_check_id = #{securityCheckId} " +
            "order by mscri.create_time ")
    Page<SecurityCheckRectificationItemRespVO> page(Page<Object> objectPage, @Param("securityCheckId") Long securityCheckId);

    @Select("SELECT DISTINCT " +
            "mscri.* " +
            "FROM " +
            "member_security_check_rectification_item mscri " +
            "LEFT JOIN member_security_check msc ON mscri.security_check_id = msc.id " +
            "LEFT JOIN member_security_check_program mscp ON mscp.id = msc.security_check_program_id " +
            "LEFT JOIN member_security_check_program_gridders mscpg on mscpg.security_check_program_id = mscp.id " +
            "WHERE " +
            "mscri.deleted = FALSE  " +
            "AND msc.deleted = FALSE  " +
            "AND mscp.deleted = FALSE " +
            "AND mscpg.deleted = FALSE " +
            "AND ${ew.sqlSegment}")
    List<SecurityCheckRectificationItemDO> getForExpiringCorrections(@Param("ew") QueryWrapper<SecurityCheckRectificationItemDO> wrapper);

    @Select("SELECT DISTINCT " +
            "mscri.id AS id, " +
            "mscp.name AS programName, " +
            "msc.check_date AS checkDate, " +
            "msc.security_check_type as securityCheckType, " +
            "msc.pitfall_level as pitfallLevel, " +
            "mscp.check_work_unit AS checkWorkUnit, " +
            "mscri.hazard_address AS hazardAddress, " +
            "mscri.hazard_context AS hazardContext, " +
            "mscri.hazard_type AS hazardType, " +
            "mscri.remediation AS remediation, " +
            "mscri.target AS target, " +
            "mscri.STATUS AS status, " +
            "mscri.capital AS capital, " +
            "msc.wilfulness AS wilfulness, " +
            "mscri.responsible_unit AS responsibleUnit, " +
            "msc.timeframe AS timeframe, " +
            "mscri.remark AS remark,  " +
            "mscri.is_overdue AS isOverdue " +
            "FROM " +
            "member_security_check_rectification_item mscri " +
            "LEFT JOIN member_security_check msc ON mscri.security_check_id = msc.id " +
            "LEFT JOIN member_security_check_program mscp ON mscp.id = msc.security_check_program_id " +
            "LEFT JOIN member_security_check_program_gridders mscpg on mscpg.security_check_program_id = mscp.id " +
            "WHERE " +
            "mscri.deleted = FALSE  " +
            "AND msc.deleted = FALSE  " +
            "AND mscp.deleted = FALSE " +
            "AND mscpg.deleted = FALSE " +
            "AND ${ew.SqlSegment}")
    Page<HazardLedgerPageRespVO> hazardLedger(Page<Object> objectPage,@Param("ew") QueryWrapper<SecurityCheckRectificationItemDO> wrapper);

    @Select("SELECT DISTINCT " +
            "mscri.id AS id, " +
            "msc.check_date AS checkDate, " +
            "mscp.check_work_unit AS checkWorkUnit, " +
            "mscri.hazard_address AS hazardAddress, " +
            "mscri.hazard_context AS hazardContext, " +
            "mscri.hazard_type AS hazardType, " +
            "mscri.remediation AS remediation, " +
            "mscri.target AS target, " +
            "mscri.STATUS AS status, " +
            "mscri.capital AS capital, " +
            "msc.wilfulness AS wilfulness, " +
            "mscri.responsible_unit AS responsibleUnit, " +
            "msc.timeframe AS timeframe, " +
            "mscri.remark AS remark,  " +
            "mscri.is_overdue AS isOverdue " +
            "FROM " +
            "member_security_check_rectification_item mscri " +
            "LEFT JOIN member_security_check msc ON mscri.security_check_id = msc.id " +
            "LEFT JOIN member_security_check_program mscp ON mscp.id = msc.security_check_program_id " +
            "WHERE " +
            "mscri.deleted = FALSE  " +
            "AND msc.deleted = FALSE  " +
            "AND mscp.deleted = FALSE " +
            "AND ${ew.SqlSegment}")
    List<HazardLedgerPageRespVO> hazardLedgerList(@Param("ew") QueryWrapper<SecurityCheckRectificationItemDO> wrapper);

    @Select("SELECT " +
            "mscri.*  " +
            "FROM " +
            "member_security_check_rectification_item mscri " +
            "LEFT JOIN member_security_check msc ON mscri.security_check_id = msc.id  " +
            "WHERE mscri.deleted = FALSE and ${ew.SqlSegment}")
    List<SecurityCheckRectificationItemDO> list(@Param("ew") QueryWrapper<SecurityCheckRectificationItemDO> wrapper);
}
